package com.example.hydrogengenerator.ext

import android.app.Activity
import android.view.View
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.SimpleItemAnimator
import androidx.viewpager.widget.ViewPager
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.chad.library.adapter.base.BaseQuickAdapter
import com.google.android.material.tabs.TabLayout
import com.kingja.loadsir.core.LoadService
import com.kingja.loadsir.core.LoadSir
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener


/**
 * 描述　:项目中自定义类的拓展函数
 */

fun loadServiceInit(view: View, callback: () -> Unit): LoadService<Any> {
    val loadsir = LoadSir.getDefault().register(view) {
        //点击重试时触发的操作
        callback.invoke()
    }
    loadsir.showSuccess()
    return loadsir
}

//绑定普通的Recyclerview
fun RecyclerView.init(
    bindAdapter: BaseQuickAdapter<*, *>,
    layoutManger: RecyclerView.LayoutManager = LinearLayoutManager(context),
    isScroll: Boolean = true,
    hasEmpty: Boolean = true,
    emptyImage: Int = -1,
    emptyText: String = "",
    emptyLayoutId: Int = -1
): RecyclerView {
    layoutManager = layoutManger
    adapter = bindAdapter
    isNestedScrollingEnabled = isScroll

    this.itemAnimator?.addDuration = 0
    this.itemAnimator?.changeDuration = 0
    this.itemAnimator?.moveDuration = 0
    this.itemAnimator?.removeDuration = 0
    (this.itemAnimator as SimpleItemAnimator?)?.supportsChangeAnimations = false

    return this
}

fun SmartRefreshLayout.init(onRefresh: () -> Unit, onLoadMore: () -> Unit) {
    setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
        override fun onLoadMore(refreshLayout: RefreshLayout) {
            onLoadMore.invoke()
        }

        override fun onRefresh(refreshLayout: RefreshLayout) {
            onRefresh.invoke()
        }
    })

}


//设置适配器的列表动画
fun BaseQuickAdapter<*, *>.setAdapterAnimation(mode: Int) {
    //等于0，关闭列表动画 否则开启
    if (mode == 0) {
        this.animationEnable = false
    } else {
        this.animationEnable = true
        this.setAnimationWithDefault(BaseQuickAdapter.AnimationType.values()[mode - 1])
    }
}


fun ViewPager2.init(
    fragment: Fragment,
    fragments: ArrayList<Fragment>,
    isUserInputEnabled: Boolean = true
): ViewPager2 {
    //是否可滑动
    this.isUserInputEnabled = isUserInputEnabled
    //设置适配器
    adapter = object : FragmentStateAdapter(fragment) {
        override fun createFragment(position: Int) = fragments[position]
        override fun getItemCount() = fragments.size
    }
    return this
}

fun ViewPager.init(
    fragment: Fragment,
    fragments: ArrayList<Fragment>
): ViewPager {
    //是否可滑动
    //设置适配器
    adapter = object : FragmentStatePagerAdapter(fragment.childFragmentManager) {
        override fun getCount(): Int = fragments.size
        override fun getItem(position: Int): Fragment = fragments[position]
    }
    return this
}

fun ViewPager2.initMain(fragment: Fragment, fragments: ArrayList<Fragment>): ViewPager2 {
    //是否可滑动
    this.isUserInputEnabled = false
    this.offscreenPageLimit = 2
    //设置适配器
    adapter = object : FragmentStateAdapter(fragment) {
        override fun createFragment(position: Int): Fragment {
            return fragments[position]
        }

        override fun getItemCount() = fragments.size
    }
    return this
}

fun TabLayout.init(navigationItemSelectedAction: (Int) -> Unit): TabLayout {
    addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
        override fun onTabReselected(tab: TabLayout.Tab?) {
        }

        override fun onTabUnselected(tab: TabLayout.Tab?) {
        }

        override fun onTabSelected(tab: TabLayout.Tab?) {
            tab?.let {
                navigationItemSelectedAction.invoke(tab.position)
            }
        }

    })
    return this
}

fun EditText.setOnSearchBtnClick(action: () -> Unit) {
    setOnEditorActionListener { v, actionId, event ->
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            action()
            true
        } else
            false
    }
}

/**
 * 隐藏软键盘
 */
fun hideSoftKeyboard(activity: Activity?) {
    activity?.let { act ->
        val view = act.currentFocus
        view?.let {
            val inputMethodManager =
                act.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
            inputMethodManager.hideSoftInputFromWindow(
                view.windowToken,
                InputMethodManager.HIDE_NOT_ALWAYS
            )
        }
    }
}

fun hideSoftKeyboard(view: EditText?) {
    view?.let {
        val inputMethodManager =
            view.context.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
        inputMethodManager.hideSoftInputFromWindow(
            view.windowToken,
            InputMethodManager.HIDE_NOT_ALWAYS
        )
    }
}
